     /*
 *@本js功能：制作章节
 *@本js作者：杨锡君
 *@编写时间：2017年10月31日
 */
$(function() {
    var Alert1 = new Alert();
    var confirm1 = new Confirm();
    var loading1 = new Loading();
    loading1.init();
    var $tableBox = $(".tableBox");
    var $chapterName = $("#departNames");
    var $searchBtn = $(".searchBtn");
    var $addChapter = $(".addChapter");
    var $addChapterBox = $(".addChapterBox");
    var $mb = $(".mb");
    var th = "<tr>" +
        "<th class=\"col-lg-1\"><input type=\"checkbox\" name=\"controlAll\" id=\"controlAll\" class=\"allCheck\"></th>" +
        "<th class=\"col-lg-4\">章节名称</th>" +
        "<th class=\"col-lg-3\">章节编号</th>" +
        "<th class=\"col-lg-2\">章节类型</th>" +
        "<th class=\"col-lg-2\">操作</th>" +
        "</tr>";

    function tabelModel(opt) {
        var str1 = "";
        var str = "<tr data-num=\"0\">" +
            "<td class=\"col-lg-1\"><input value=\"0\" data-chapterid=" + opt.chapterId + " name=\"selecteds\" type=\"checkbox\" class=\"oneCheck\" /></td>" +
            "<td class=\"col-lg-6\" title=" + opt.chapterName + ">" + opt.chapterName + "</td>"+
            "<td class=\"col-lg-6\" title=" + opt.courseMark + ">" + opt.courseMark + "</td>";
        str1 = "<td class=\"col-lg-3\" title=\"实验课\">实验课</td>";
        if(opt.addTestMark == 1) {
            str1 = "<td class=\"col-lg-3\" title=\"实验课\">实验课</td>";
        }
        if(opt.addTestMark == 2) {
            str1 = "<td class=\"col-lg-3\" title=\"理论课\">理论课</td>";
        }
        if(opt.addTestMark == 3) {
            str1 = "<td class=\"col-lg-3\" title=\"理论及实验课\">理论及实验课</td>";
        }

        var str2 = "<td class=\"col-lg-2\">" +
            "<a data-chapterid=" + opt.chapterId + "  href=\"javascript:;\" class=\"repairBtn\">信息修改</a>" +
            "<a data-chapterid=" + opt.chapterId + "  href=\"javascript:;\" class=\"repairBtns\">内容修改</a>" +
            "</td>" +
            "</tr>";
        return str + str1 + str2;
    }
    var addChapterModel = "<div class=\"popup addChapterBox\">" +
        "<div class=\"popup_box\">" +
        "<div class=\"popup_contenthead\">添加章节<img class=\"popup_img\" src=\"pc/images/ico_alertDel.png\"></div>" +
        "<div class=\"popup_content\">" +
        "<div>章节名称：<input style='width: 242px;' autocomplete='off' maxlength=\"50\" type=\"text\" class=\"publicText chapterNames\" id=\"chapterName\" name=\"chapterName\" /></div>" +
        "<div>章节类型：<select style='width: 256px;'  class=\"courseType publicSelect\">" +
        "<option value=\"2\">理论课</option>" +
        "<option value=\"1\">实验课</option>" +
        "<option value=\"3\">理论及实验课</option>" +
        "</select></div>" +
        "<div class=\"popup_btn\">" +
        "<a class=\"popup_confirm exportBtn hover-btn publicBtn\">确定</a>" +
        "<a class=\"popup_close exportCancle hover-btn\">取消</a>" +
        "</div>" +
        "</div>" +
        "<div class=\"clear\"></div>" +
        "</div>" +
        "</div>";
    var editChapterModel = "<div class=\"popup addChapterBox\">" +
        "<div class=\"popup_box\">" +
        "<div class=\"popup_contenthead\">修改章节<img class=\"popup_img\" src=\"pc/images/ico_alertDel.png\"></div>" +
        "<div class=\"popup_content\">" +
        "<div>章节名称：<input style='width: 242px;' autocomplete='off' maxlength=\"50\" type=\"text\" class=\"publicText chapterNames\" id=\"chapterName\" name=\"chapterName\" /></div>" +
        "<div>章节类型：<select style='width: 256px;' class=\"courseType publicSelect\">" +
        "<option value=\"2\">理论课</option>" +
        "<option value=\"1\">实验课</option>" +
        "<option value=\"3\">理论及实验课</option>" +
        "</select>"+
        "<div class=\"prompt\">" +
        "<!--提示-->" +
        "<span style=\"font-size: 12px;\">提示 :&ensp;<i style='color: #b8b8b8;'>切换的 '章节类型' 若不包含当前'章节类型' 的内容，将会删除当前 '章节类型' 的资源。</i></span>" +
        "</div>"+
        "</div>" +
        "<div class=\"popup_btn\">" +
        "<a class=\"popup_confirm exportBtn hover-btn publicBtn\">确定</a>" +
        "<a class=\"popup_close exportCancle hover-btn\">取消</a>" +
        "</div>" +
        "</div>" +
        "<div class=\"clear\"></div>" +
        "</div>" +
        "</div>";

    var chapterMake = {
        some:'',
        init: function() {
            this.bindEvent();
            maxLengthLimit.init("publicText");
        },
        bindEvent: function() {
            var self = this;
            var json = {
                "pageNow": 1,
                "pageSize": 10
            };
            if(result.paramData!=undefined){
                if(result.paramData.some!=undefined){
                    json = result.paramData.some;
                    if(json.chapterName!=undefined){
                        $chapterName.val(json.chapterName)
                    }
                    if(json.addTestMark != undefined){
                        $("#chapterType").val(json.addTestMark)
                    }
                }
            }
            self.some = json;
            //获取列表
            self.tabelList(json);

            //			添加
            $addChapter.off("click").on("click", function() {
                self.addChapterEvent();
                maxLengthLimit.init("publicText");
            });
            //删除
            $(".delChapter").off("click").on("click", function() {
                self.delListEvent();
            });
            //搜索
            $searchBtn.off("click").on("click", function() {
                var chapterName = $chapterName.val();
                // var flag = verification.spaceCode(chapterName);
                var flags = verification.specialCode(chapterName);
                // if(flag != true) {
                //     Alert1.content = flag;
                //     $chapterName.val("");
                //     Alert1.init();
                //     Alert1.show();
                //     return false;
                // }
                if(flags != true) {
                    Alert1.content = flags;
                    $chapterName.val("");
                    Alert1.init();
                    Alert1.show();
                    return false;
                }
                var jsonStr = {
                    "pageNow": 1,
                    "pageSize": 10
                };
                if($("#chapterType").val() != 0){
                    jsonStr.addTestMark =  Number($("#chapterType").val());
                }
                if(chapterName == "") {
                    chapterName = "";
                } else {
                    jsonStr.chapterName = chapterName;
                }
                self.tabelList(jsonStr);
                self.some = jsonStr;
            });

        },
        addChapterEvent: function() {
            var self = this;
            $mb.show();
            $mb.after(addChapterModel);
            // 取消
            $(".exportCancle ").off("click").click(function() {
                $mb.hide();
                $(".popup").remove();
            });
            $(".popup_img ").off("click").click(function() {
                $mb.hide();
                $(".popup").remove();
            });
            //添加章节确认按钮
            $(".exportBtn").off("click").click(function () {
                var jsonStr = JSON.stringify({"chapterName":$(".chapterNames").val(),"addTestMark":$(".courseType").val()});
                var flag = self.exportVerficateEvent();
                if(flag){
                    loading1.show();
                    $.ajax({
                        url: 'addChapterTopOne',
                        type: 'post',
                        async: true,
                        data:{
                            "jsonStr":jsonStr
                        },
                        success: function (msg) {
                            loading1.hide();
                            msg = JSON.parse(msg);
                            if(msg.result == "success"){
                                var json = JSON.stringify({
                                    "pageName":"chapterAdd",
                                    "paramData":{
                                        "chapterId":msg.chapterId,
                                        "some":self.some
                                    }
                                });
                                window.location.href = "skipPage?jsonStr="+json;
                            }else{
                                if(msg.failMsg!=undefined){
                                    Alert1.content = msg.failMsg;
                                }else{
                                    Alert1.content = "保存失败";
                                }
                                Alert1.init();
                                Alert1.show();
                            }
                        },
                        error:function () {
                            var json = JSON.stringify({
                                "pageName": "error"
                            });
                            window.location.href = "skipPage?jsonStr=" + json;
                        }
                    });
                }

            });

        },
        exportVerficateEvent: function() {
            //添加章节确认验证
            var chapterName = $(".chapterNames").val();
            if(chapterName == "") {
                Alert1.content = "请输入章节名称";
                Alert1.init();
                Alert1.show();
                return false;
            }
            // var flag = verification.spaceCode(chapterName);
            var flags = verification.specialCode(chapterName);
            // if(flag != true) {
            //     Alert1.content = flag;
            //     $(".chapterNames").val("");
            //     Alert1.init();
            //     Alert1.show();
            //     return false;
            // }
            if(flags != true) {
                Alert1.content = flags;
                $(".chapterNames").val("");
                Alert1.init();
                Alert1.show();
                return false;
            }
            return true;
        },
        editEvent:function () {
            var self = this;
            //内容修改
            $(".repairBtns").off("click").click(function () {
                var json = JSON.stringify({
                    "pageName":"chapterEdit",
                    "paramData":{
                        "chapterId":Number($(this).attr("data-chapterid")),
                        "some":self.some
                    }
                });
                window.location.href = "skipPage?jsonStr="+json;
            });
            //信息修改
          $(".repairBtn").off("click").click(function () {
              var id = Number($(this).attr("data-chapterid"));
              $mb.show();
              $mb.after(editChapterModel);
              var json = JSON.stringify({"chapterId":id});
              $.ajax({
                  type:'post',
                  url:'skipSaveChapter',
                  async:true,
                  data:{
                      "jsonStr":json
                  },
                  success:function (msg) {
                      msg = JSON.parse(msg);
                      if(msg.result == "success"){
                          $("#chapterName").val(msg.chapterMsg.chapterName);
                          $(".courseType").val(msg.chapterMsg.addTestMark);
                      }else{
                          if(msg.failMsg!=undefined){
                              Alert1.content = msg.failMsg;
                          }else{
                              Alert1.content = "保存失败";
                          }
                          Alert1.init();
                          Alert1.show();
                      }
                  }
              });
              // 取消
              $(".exportCancle ").off("click").click(function() {
                  $mb.hide();
                  $(".popup").remove();
              });
              $(".popup_img ").off("click").click(function() {
                  $mb.hide();
                  $(".popup").remove();
              });
              //添加章节确认按钮
              $(".exportBtn").off("click").click(function () {
                  var jsonStr = JSON.stringify({"chapterName":$("#chapterName").val(),"addTestMark": $(".courseType").val(),"chapterId":id});
                  var flag = self.exportVerficateEvent();
                  if(flag){
                      loading1.show();
                      $.ajax({
                          url: 'saveChapterTop',
                          type: 'post',
                          async: true,
                          data:{
                              "jsonStr":jsonStr
                          },
                          success: function (msg) {
                              loading1.hide();
                              msg = JSON.parse(msg);
                              if(msg.result == "success"){
                                  Alert1.content = "保存成功";
                                  Alert1.success = function () {
                                      $mb.hide();
                                      $(".popup").remove();
                                      self.tabelList(self.some);
                                  };
                                  Alert1.init();
                                  Alert1.show();
                              }else{
                                  if(msg.failMsg!=undefined){
                                      Alert1.content = msg.failMsg;
                                  }else{
                                      Alert1.content = "保存失败";
                                  }
                                  Alert1.init();
                                  Alert1.show();
                              }
                          },
                          error:function () {
                              var json = JSON.stringify({
                                  "pageName": "error"
                              });
                              window.location.href = "skipPage?jsonStr=" + json;
                          }
                      });
                  }

              });
          });
        },
        tabelList: function(jsons) {
//			列表获取
            var self = this;
            var json = JSON.stringify(jsons);
            loading1.show();
            $.ajax({
                type: "post",
                url: "getChapterList",
                data: {
                    "jsonStr": json
                },
                async: true,
                success: function(msg) {
                    msg = JSON.parse(msg);
                    loading1.hide();
                    $tableBox.empty();
                    if(msg.result == "success"){
                        if(msg.chapterList.length != 0) {
                            //列表
                            var tabel = "";
                            $.map(msg.chapterList, function(opt) {
                                tabel += tabelModel(opt);
                            });
                            $tableBox.append(th + tabel);
                            self.allCheckEvent();
                            self.editEvent();
                        } else {
                            var trImg = "<tr><td colspan='6'><img style='margin: 80px auto 20px;' src='pc/images/ico_noneData.png'><p style='font-size:26px;color:#8a8a8a;width:100%;text-align:center;'>暂无数据</p></td></tr>";
                            $tableBox.append(th + trImg)
                        }
                        var sumpage = msg.totalPage;
                        if(sumpage == 0){
                            sumpage = 1;
                        }
                        var nowpage = msg.pageNow;
                        var pagenumber = 10;
                        $(".tcdPageCode").createPage({
                            pageCount: sumpage, //总页数
                            current: nowpage, //当前页数
                            pageNumber: pagenumber, //每页显示数量
                            backFn: function(p) {
                                nowpage = p;
                                if(nowpage >= sumpage) {
                                    nowpage = sumpage;
                                }
                                var jsonStr = jsons;
                                jsonStr.pageNow = nowpage;
                                loading1.show();
                                self.some.pageNow = nowpage;
                                $.ajax({
                                    type: "post",
                                    url: "getChapterList",
                                    data: {
                                        "jsonStr": JSON.stringify(jsonStr)
                                    },
                                    async: true,
                                    success: function(msg) {
                                        msg = JSON.parse(msg);
                                        loading1.hide();
                                        //列表
                                        $tableBox.empty();
                                        var tabel = "";
                                        $.map(msg.chapterList, function(opt) {
                                            tabel += tabelModel(opt);
                                        });
                                        $tableBox.append(th + tabel);
                                        self.allCheckEvent();
                                        self.editEvent();
                                    }
                                });
                            }
                        });
                    }else{
                        if(msg.failMsg != undefined){
                            if(msg.failMsg == "暂无权限"){
                                parent.location.reload(true);
                            }else{
                                Alert1.content = msg.failMsg;
                                Alert1.init();
                                Alert1.show();
                            }
                        }else{
                            if (msg.errorMsg != "" && msg.errorMsg != undefined) {
                                Alert1.content = msg.errorMsg;
                            } else {
                                Alert1.content = "获取失败";
                            }
                            Alert1.init();
                            Alert1.show();
                        }
                    }
                }
            });

        },
        delListEvent: function() {
            var self = this;
            if($(".oneCheck[value=1]").length == 0) {
                Alert1.content = "请选择要删除的章节";
                Alert1.init();
                Alert1.show();
            } else {
                var list = [];
                for(var i = 0; i < $(".oneCheck[value=1]").length; i++) {
                    list.push($(".oneCheck[value=1]").eq(i).attr("data-chapterid"));
                }
                confirm1.content = "确定删除这<a style='color:#c82011;text-decoration:underline;font-weight:bolder;' href='javascript:;'>" + $(".oneCheck[value=1]").length + "</a>个章节吗"
                confirm1.success = function() {
                    loading1.show();
                    var jsonStr = JSON.stringify(list);
                    $.ajax({
                        type: "post",
                        url: "delChapters",
                        async: true,
                        data: {
                            "jsonStr": jsonStr
                        },
                        success: function(msg) {
                            msg = JSON.parse(msg);
                            loading1.hide();
                            if(msg.result == "success"){
                                Alert1.content = "删除成功";
                                Alert1.success = function () {
                                    self.tabelList(self.some);
                                };
                                Alert1.init();
                                Alert1.show();
                            }else{
                                Alert1.content = msg.failMsg;
                                Alert1.init();
                                Alert1.show();
                            }

                        },
                        error: function(err) {

                        }

                    });
                };
                confirm1.init();
                confirm1.show();

            }
        },
        allCheckEvent: function() {
            //全选
            allCheck.init("controlAll", "oneCheck");
        }
    };
    chapterMake.init();
});